package com.hq.ims.data.service;

import com.hq.ims.data.dao.CompanyRuleHistoryMapper;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.CompanyChangeHistory;
import com.hq.ims.data.entity.CompanyRuleHistory;
import com.hq.ims.data.entity.FileRecord;
import com.hq.ims.data.entityenum.EnumDataType;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.AppFileMapstruct;
import com.hq.ims.data.vo.CompanyChangeHistoryMapstruct;
import com.hq.ims.data.vo.CompanyMapstruct;
import com.hq.ims.data.vo.CompanyRuleHistoryMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.exception.CommonException;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author jiang bai ping
 * @Email 526666031@qq.com
 * @DateTime 2024-10-28 09:05:29
 * @Detail 企业信息服务实现类
 */
@Service
public class BaseCompanyRuleHistoryService extends BaseService<CompanyRuleHistoryMapper, CompanyRuleHistory> {

    @Resource
    private BaseCompanyChangeHistoryService baseCompanyChangeHistoryService;

//    public List<CompanyRuleHistory> getCompanyAllRuleList(String id, EnumDataType enumDataType) {
//        return this.lambdaQuery().eq(CompanyRuleHistory::getCompanyCheckId, id)
//                .eq(CompanyRuleHistory::getDataType, enumDataType)
//                .orderByDesc(CompanyRuleHistory::getCreateDateTime)
//                .list();
//    }

//    public ReturnPage getCompanyAllRulePage(RequestPage page, CompanyMapstruct.PageSearchVo searchVo) {
//
//
//        List<String> companyIds = new ArrayList<>();
//        if (StringUtils.isNotEmpty(searchVo.getCompanyCardNumber())){
//
//            List<Company> companies = baseCompanyService.lambdaQuery().like(Company::getCompanyCardNumber, searchVo.getCompanyCardNumber()).list();
//            if (ListUtils.isExistsEle(companies)){
//                List<String> list = companies.stream().map(Company::getCompanyId).collect(Collectors.toList());
//                companyIds.addAll(list);
//            }
//            else {
//                return new ReturnPage();
//            }
//        }
//
//        if (StringUtils.isNotEmpty(searchVo.getParentId())){
//            List<Company> childrenCompanyList = baseCompanyService.getAllChildrenCompanyList(searchVo.getParentId(), true);
//            List<String> strings = childrenCompanyList.stream().map(Company::getCompanyId).collect(Collectors.toList());
//            companyIds.addAll(strings);
//        }
//
//        RequestPage requestPage = this.lambdaQuery()
//                .eq(CompanyRuleHistory::getDataType, EnumDataType.After)
//                .eq(searchVo.getAuditStatus() != null, CompanyRuleHistory::getAuditStatus, searchVo.getAuditStatus())
//                .like(StringUtils.isNotEmpty(searchVo.getCompanyName()), CompanyRuleHistory::getCompanyName, searchVo.getCompanyName())
//                .like(StringUtils.isNotEmpty(searchVo.getRuleRate()), CompanyRuleHistory::getCompanyDivideIntoRuleValue, searchVo.getRuleRate())
//                .in(ListUtils.isExistsEle(companyIds), CompanyRuleHistory::getCompanyId, companyIds)
//                .between(searchVo.getStartDate() != null, CompanyRuleHistory::getUpdateDateTime, searchVo.getStartDate(), searchVo.getEndDate())
//                .orderByDesc(CompanyRuleHistory::getCreateDateTime)
//                .page(page);
//
//        List<CompanyRuleHistoryMapstruct.CompanyRuleHistoryVo> ruleHistories = JsonUtils.convertListObject(requestPage.getRecords(), CompanyRuleHistoryMapstruct.CompanyRuleHistoryVo.class);
//
//        if (ListUtils.isExistsEle(ruleHistories)){
//            for (CompanyRuleHistoryMapstruct.CompanyRuleHistoryVo ruleHistory : ruleHistories) {
//                //文件
//                List<FileRecord> serviceFile = baseFileRecordService.getFile(ruleHistory.getCompanyCheckId(), LocalConstant.FILE_COMPANY_DIVIDE_INTO_RULE);
//                List<AppFileMapstruct.FileVo> fileVos = baseFileRecordService.changeFileRecordToFileVo(serviceFile);
//                ruleHistory.setFiles(fileVos);
//
//                //上级组织
//                Company company = baseCompanyService.getById(ruleHistory.getCompanyId());
//                if (company != null){
//                    ruleHistory.setParentName(company.getParentName());
//                    ruleHistory.setCompanyCardNumber(company.getCompanyCardNumber());
//                }
//
//
//
//                CompanyChangeHistory changeHistory = baseCompanyChangeHistoryService.getById(ruleHistory.getCompanyChangeId());
//                if (changeHistory != null){
//                    ruleHistory.setRejectReason(changeHistory.getRejectReason());
//                }
//            }
//
//            requestPage.setRecords(ruleHistories);
//        }
//
//        return requestPage.getReturnPage();
//    }

    public CompanyRuleHistory getRuleHistoryInfoByCheckId(String id, Integer dataType) {
        return this.lambdaQuery().eq(CompanyRuleHistory::getCompanyCheckId, id).eq(CompanyRuleHistory::getDataType, dataType).one();
    }
}
